package com.teemo.equipment.domain.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teemo.equipment.converter.BasePageVOMapStruct;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Teemo
 */
@Data
public class BasePageVO<T> implements Serializable {

    @Schema(description = "页码")
    private Long pageNum;
    @Schema(description = "每页数量")
    private Long pageSize;
    private List<T> datas;
    @Schema(description = "总页数")
    private Long total;

    public static <T,E> BasePageVO<T> convert(Page<E> page, Function<E,T> function) {
        List<E> records = page.getRecords();
        BasePageVO vo=  BasePageVOMapStruct.INSTANCE.toBasePageVO(page);
        if (CollectionUtil.isEmpty(records)){
            return vo;
        }
        List<T> datas = records.stream().map(function).collect(Collectors.toList());
        vo.setDatas(datas);
        return vo;
    }
}
